package cn.aloxc.tianya.reader;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TabHost.OnTabChangeListener;
public class Activity_test_tabview extends TabActivity{
	private Button call_button = null;
	private EditText call_text = null;
	protected int myMenuSettingTag=0;
	protected Menu myMenu;
	private TabHost host = null;
	final int msn = R.drawable.msn;
	final int refresh = R.drawable.refresh;
	final int back = R.drawable.back;
	private String dialogTitle = null;
	private static final int myMenuResources[] = { R.menu.activity_test_tabview_menu1,
		R.menu.activity_test_tabview_menu2, R.menu.activity_test_tabview_menu3};
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle data = this.getIntent().getExtras();
		setTitle(data.getString("testItem"));
		int width = 45;
		int height = 60;
		host = this.getTabHost();
		final TabWidget tabWidget = host.getTabWidget();
		LayoutInflater.from(this).inflate(R.layout.activity_test_tabview,
				host.getTabContentView(), true);
		host.setBackgroundResource(R.color.red);
		host.addTab(host.newTabSpec("tab1").setIndicator("phone",getResources().getDrawable(R.drawable.phone)).setContent(
				R.id.test_tabview_tab_first));
		host.addTab(host.newTabSpec("tab2").setIndicator("photo",getResources().getDrawable(R.drawable.photo)).setContent(
				R.id.test_tabview_tab_second));
		host.addTab(host.newTabSpec("tab3").setIndicator("flash",getResources().getDrawable(R.drawable.flash)).setContent(
				R.id.test_tabview_tab_third));

		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			/**
			 * 设置高度、宽度，不过宽度由于设置为fill_parent，在此对它没效果
			 */
			tabWidget.getChildAt(i).getLayoutParams().height = height;
			tabWidget.getChildAt(i).getLayoutParams().width = width;
		}
		host.setOnTabChangedListener(new OnTabChangeListener(){
			public void onTabChanged(String arg0) {
				int c = host.getCurrentTab();
				View v = tabWidget.getChildAt(c);
				switch(c)
				{
					case 0:
						myMenuSettingTag = 1;
						v.setBackgroundDrawable(Activity_test_tabview.this.getResources().getDrawable(R.color.red));
						break;
					case 1:
						myMenuSettingTag = 2;
						v.setBackgroundDrawable(Activity_test_tabview.this.getResources().getDrawable(R.color.green));
						break;
					case 2:
						myMenuSettingTag = 3;
						v.setBackgroundDrawable(Activity_test_tabview.this.getResources().getDrawable(R.color.blue));
						break;
				}
			}});
		
		call_button = (Button)findViewById(R.id.test_tabview_tab_first_button);
		call_text = (EditText)findViewById(R.id.test_tabview_tab_first_edittext);
		call_button.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View button) {
				CharSequence number = call_text.getText();
				Intent callIntent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + number));
				startActivity(callIntent);
			}});
	}
	public boolean onPrepareOptionsMenu(Menu menu){
		// Hold on to this
		myMenu = menu;
		myMenu.clear();//清空MENU菜单
		MenuInflater inflater = getMenuInflater();        
        //从TabActivity这里获取一个MENU过滤器
		switch (myMenuSettingTag) {
		case 1:
			inflater.inflate(myMenuResources[0], menu);
            //动态加入数组中对应的XML MENU菜单
			break;
		case 2:
			inflater.inflate(myMenuResources[1], menu);
			break;
		case 3:
			inflater.inflate(myMenuResources[2], menu);
			break;
		default:
			inflater.inflate(myMenuResources[0], menu);
			break;
		}
		return super.onCreateOptionsMenu(menu);
	}
	public boolean onOptionsItemSelected(MenuItem item) {
		String title = (String) item.getTitle();
		if(title.equals(this.getString(R.string.test_tabview_menu_title_back))){
			this.dialogTitle = title;
			showDialog(back);
		}else if(title.equals(this.getString(R.string.test_tabview_menu_title_refresh))){
			this.dialogTitle = title;
			showDialog(refresh);
		}else if(title.equals(this.getString(R.string.test_tabview_menu_title_msn))){
			this.dialogTitle = title;
			showDialog(msn);
		}
		return super.onOptionsItemSelected(item);
	}
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new Builder(Activity_test_tabview.this);
		builder.setIcon(id);
		builder.setTitle(this.dialogTitle);
		builder.setPositiveButton(R.string.test_button_text_ok,new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface arg0, int arg1) {
			}
		});
		builder.setNegativeButton(R.string.test_button_text_cencel, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int which) {
		}});
		return builder.create();
	}
}
